package com.me.blood.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

import androidx.annotation.Nullable;

import com.me.ad.ADType;
import com.me.blood.databinding.ActivityFeedbackBinding;
import com.me.blood.frag.BaseDialogFragment;
import com.me.blood.frag.FeedDlg;
import com.me.utils.ToastUtil;
import com.plata.base.utils.WeakHandler;

public class FeedbackActivity extends BaseActivity {

    ActivityFeedbackBinding binding;
    int type = -1;
    WeakHandler h = new WeakHandler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFeedbackBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                binding.tv.setText(s.length() + "/100");
            }
        });
        binding.back.setOnClickListener(v -> finish());
        binding.ok.setOnClickListener(v -> {
            save();
        });
        binding.type.setOnClickListener(v -> {
            FeedDlg dlg = new FeedDlg();
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk && value instanceof Integer) {
                        type = (Integer) value;
                        switch (type) {
                            case 0:
                                binding.type.setText("产品建议");
                                break;
                            case 1:
                                binding.type.setText("功能bug");
                                break;
                            case 2:
                                binding.type.setText("会员充值");
                                break;
                            default:
                                binding.type.setText("其他");
                        }
                    }
                }
            });
            dlg.show(getSupportFragmentManager(), "type");
        });
    }

    private void save() {
        if (type == -1) {
            ToastUtil.showShortToast("请选择反馈类型");
            return;
        }
        String msg = binding.et.getText().toString().trim();
        if (TextUtils.isEmpty(msg)) {
            ToastUtil.showShortToast("请描述您的建议或问题");
            return;
        }
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showShortToast("已提交");
                finish();
            }
        }, 500);
    }

    @Override
    protected ADType getXpType() {
        return ADType.XP;
    }
}
